package wojtek.tablice;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class SixpackGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField txtPozostale;
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnNewRadioButton_1;
	private JRadioButton rdbtnNewRadioButton_2;
	private JRadioButton rdbtnNewRadioButton_3;
	private JRadioButton rdbtnNewRadioButton_4;
	private JRadioButton rdbtnNewRadioButton_5;
	Sixpack danaPaka;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Sixpack paka = new Sixpack();
					SixpackGUI frame = new SixpackGUI(paka);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SixpackGUI(final Sixpack danaPaka) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		txtPozostale = new JTextField();
		txtPozostale.setText("Zostalo: " + Integer.toString(danaPaka.stan()) + " piw!");
		contentPane.add(txtPozostale, "6, 2, fill, default");
		txtPozostale.setColumns(10);
		txtPozostale.setEditable(false);
		
		
		
		rdbtnNewRadioButton = new JRadioButton(danaPaka.getZawartosc()[0][0].getNazwaPiwa() );
		buttonGroup.add(rdbtnNewRadioButton);
		contentPane.add(rdbtnNewRadioButton, "4, 4");
		
		rdbtnNewRadioButton_3 = new JRadioButton(danaPaka.getZawartosc()[0][2].getNazwaPiwa());
		buttonGroup.add(rdbtnNewRadioButton_3);
		contentPane.add(rdbtnNewRadioButton_3, "8, 4");

		
		rdbtnNewRadioButton_1 = new JRadioButton(danaPaka.getZawartosc()[0][1].getNazwaPiwa());
		buttonGroup.add(rdbtnNewRadioButton_1);
		contentPane.add(rdbtnNewRadioButton_1, "4, 6");
		
		rdbtnNewRadioButton_4 = new JRadioButton(danaPaka.getZawartosc()[1][1].getNazwaPiwa());
		buttonGroup.add(rdbtnNewRadioButton_4);
		contentPane.add(rdbtnNewRadioButton_4, "8, 6");
		
				
		rdbtnNewRadioButton_2 = new JRadioButton(danaPaka.getZawartosc()[0][2].getNazwaPiwa() );
		buttonGroup.add(rdbtnNewRadioButton_2);
		contentPane.add(rdbtnNewRadioButton_2, "4, 8");
		
		rdbtnNewRadioButton_5 = new JRadioButton(danaPaka.getZawartosc()[1][2].getNazwaPiwa());
		buttonGroup.add(rdbtnNewRadioButton_5);
		contentPane.add(rdbtnNewRadioButton_5, "8, 8");

		final JButton btnWePiwo = new JButton("we\u017A piwo");
		contentPane.add(btnWePiwo, "6, 10");
		
		btnWePiwo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(danaPaka.stan()==1){
					btnWePiwo.setText("MUSISZ KUPIC NOWE PIWA ;)");
		}
				wyborPiwka(danaPaka);
		sprawdzPaka(danaPaka); 

			}
	});
	}
	
	
	
	void wyborPiwka(Sixpack danaPaka){
		if (rdbtnNewRadioButton.isSelected() == true){
		//	System.out.println("aada" + rdbtnNewRadioButton.isSelected());
			danaPaka.setWybranePiwo(danaPaka.getZawartosc()[0][0]);
			//System.out.println(" " + danaPaka.getWybranePiwo().getNazwaPiwa());
			danaPaka.wezPiwo(danaPaka.wybranePiwo);
			//System.out.println(danaPaka.getZawartosc()[0][0].getNazwaPiwa());
		}
		else if(rdbtnNewRadioButton_1.isSelected() == true){
			danaPaka.setWybranePiwo(danaPaka.getZawartosc()[0][1]);
		danaPaka.wezPiwo(danaPaka.getWybranePiwo());	
		}
		else if(rdbtnNewRadioButton_2.isSelected() == true){
			danaPaka.setWybranePiwo(danaPaka.getZawartosc()[0][2]);
			danaPaka.wezPiwo(danaPaka.getWybranePiwo());	}
		else if(rdbtnNewRadioButton_3.isSelected() == true){
			danaPaka.setWybranePiwo(danaPaka.getZawartosc()[1][0]);
			danaPaka.wezPiwo(danaPaka.getWybranePiwo());	}
		else if(rdbtnNewRadioButton_4.isSelected() == true){
			danaPaka.setWybranePiwo(danaPaka.getZawartosc()[1][1]);
			danaPaka.wezPiwo(danaPaka.getWybranePiwo());	}
		else if(rdbtnNewRadioButton_5.isSelected() == true){
			danaPaka.setWybranePiwo(danaPaka.getZawartosc()[1][2]);
			danaPaka.wezPiwo(danaPaka.getWybranePiwo());	
			}
	}
	
	

	void sprawdzPaka(Sixpack danaPaka){
		if (danaPaka.zawartosc[0][0] != null){
			rdbtnNewRadioButton.setText(danaPaka.getZawartosc()[0][0].getNazwaPiwa());
		}else{
			rdbtnNewRadioButton.setVisible(false);
		}
		if (danaPaka.zawartosc[0][1] != null){
			rdbtnNewRadioButton_1.setText(danaPaka.getZawartosc()[0][1].getNazwaPiwa());
			}else{
				rdbtnNewRadioButton_1.setVisible(false);
			}
		if (danaPaka.zawartosc[0][2] != null){
			rdbtnNewRadioButton_2.setText(danaPaka.getZawartosc()[0][2].getNazwaPiwa());
				}else{
					rdbtnNewRadioButton_2.setVisible(false);
				}
				if (danaPaka.getZawartosc()[1][0] != null){
			rdbtnNewRadioButton_3.setText(danaPaka.getZawartosc()[1][0].getNazwaPiwa());
				}else{
					rdbtnNewRadioButton_3.setVisible(false);
				}
		if (danaPaka.getZawartosc()[1][1] != null){
			rdbtnNewRadioButton_4.setText(danaPaka.getZawartosc()[1][1].getNazwaPiwa());
				}else{
					rdbtnNewRadioButton_4.setVisible(false);
				}
		if (danaPaka.zawartosc[1][2] != null){
			rdbtnNewRadioButton_5.setText(danaPaka.getZawartosc()[1][2].getNazwaPiwa());
				}else{
					rdbtnNewRadioButton_5.setVisible(false);
				}
		txtPozostale.setText("Zostalo: " + Integer.toString(danaPaka.stan()) + " piw!");
		
	}
}
